/**
 * 
 */
package com.heer.RD.projecthour.page;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.wicket.PageParameters;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.OnChangeAjaxBehavior;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.HiddenField;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.util.value.ValueMap;
import org.apache.wicket.validation.validator.PatternValidator;
import org.apache.wicket.validation.validator.StringValidator;
import org.apache.wicket.validation.validator.UrlValidator;

import com.heer.RD.projecthour.component.DeptChoice;
import com.heer.RD.projecthour.component.StaffChoice;
import com.heer.RD.projecthour.pojo.ProjMilestoneDTO;
import com.heer.RD.projecthour.pojo.ProjectDTO;
import com.heer.RD.projecthour.pojo.UserProjDTO;
import com.heer.RD.util.SecurityFacade;
import com.heer.RD.util.UtilsMisc;
import com.heer.joss.persistence.convention.ExpressionUtil;


/**
 * @author ZhaoZehui
 *
 */
public class ProjectMngPage extends BasePage {

	public final List<Map> projMngList = new ArrayList();
	final WebMarkupContainer operPanel = new WebMarkupContainer("operPanel");

	/**
	 * 
	 */
	public ProjectMngPage(PageParameters para) {
		
		final Form f = new Form("thisForm", new CompoundPropertyModel(new ValueMap()));
		
		operPanel.setOutputMarkupId(true);
		operPanel.add(new ProjectListPanel("operPanelContent"));
		add(operPanel);
		
		final WebMarkupContainer newProjPanel = new WebMarkupContainer("newProjPanel");
		newProjPanel.setOutputMarkupId(true);
		
	
		final AjaxButton newProjButt = new AjaxButton("newProjButt"){

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form form) {
				
				operPanel.replace(new ProjectUpdatePanel("operPanelContent", null));
				target.addComponent(operPanel);
//				target.appendJavascript("$('#depts option')[0].selected = true;");
			}
		};
		
		newProjPanel.add(newProjButt);
		f.add(newProjPanel);
		
		
		final WebMarkupContainer deptPanel = new WebMarkupContainer("deptPanel");
		final DeptChoice depts = new DeptChoice("depts", getMainService().getJdbcTemplate(), getUser().getDeptId());
		depts.setOutputMarkupId(true);
		depts.setMarkupId("depts");
		deptPanel.setOutputMarkupId(true);
		
		if(!SecurityFacade.isManager()) {
			newProjButt.setVisible(false);
			depts.setVisible(false);
			newProjPanel.setVisible(false);
		}
		
		final AjaxButton queryProjButt = new AjaxButton("queryProjButt"){

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form form) {
				
				projMngList.clear();	
				String deptId = depts.getModelValue();
				
				String criteria = "1=2";
				
				if(SecurityFacade.isManager()) {
					if(StringUtils.isNotBlank(deptId) && !"-1".equals(deptId)) {
						criteria = " p.dept_id in ('"+StringUtils.join(getMainService().getSubDepts(deptId), "','")+"')";
					}
				}
				else if(SecurityFacade.isPM()) {
					criteria = " p.project_id in (select project_id from user_proj where user_id = '"+getUser().getUserId()+"')";
				}
				criteria += " and p.father_proid is null";//只显示第一级项目
				
				Map paramMap = UtilsMisc.toMap("uh_criteria", "1=1", "criteria", criteria);
				
				String sql = ExpressionUtil.parse(getMainService().getProjCostStatSql(), paramMap);
				projMngList.addAll(getMainService().getJdbcTemplate().queryForList(sql));
				
				operPanel.replace(new ProjectListPanel("operPanelContent"));
				target.addComponent(operPanel);
				
			}
		};
		f.add(queryProjButt);

		OnChangeAjaxBehavior changeDept = new OnChangeAjaxBehavior() {
			protected void onUpdate(AjaxRequestTarget target) {
			}
		};
		
		
		depts.add(changeDept);
		deptPanel.add(depts);
        f.add(deptPanel);
		add(f);
		
	}
	
	protected class ProjectUpdatePanel extends Panel {
		
		private final ProjectDTO bean = new ProjectDTO();
		ProjectDTO proj = new ProjectDTO();
		
		public ProjectUpdatePanel(String id, final String projId) {
			super(id);
			
			if(StringUtils.isNotEmpty(projId)) {
				proj = (ProjectDTO)getMainService().getProjDao().findById(projId);
			}
			
			final FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
			feedbackPanel.setOutputMarkupId(true).setMarkupId("feedbackPanel");
			
			Label selectedDate = new Label("selectedDate", "新增项目");
			selectedDate.setMarkupId("selectedDate");
			selectedDate.setOutputMarkupId(true);
			
			if(StringUtils.isNotBlank(proj.getProjectId())) {
				selectedDate.setDefaultModel(new Model("修改["+proj.getProjName()+"]"));
			}

			final HiddenField deptIdInput = new HiddenField("deptId");
			deptIdInput.setOutputMarkupId(true);
			final RequiredTextField deptNameInput = new RequiredTextField("deptName");
			deptNameInput.setOutputMarkupId(true);
			final RequiredTextField projName = new RequiredTextField("projName", new Model(proj.getProjName()));
			projName.add(StringValidator.maximumLength(100));
			final HiddenField projectId = new HiddenField("projectId", new Model(proj.getProjectId()));
			final TextField taskAddr = new TextField("taskAddr", new Model(proj.getTaskAddr()));
			taskAddr.add(new UrlValidator());
			final RequiredTextField beginDate = new RequiredTextField("beginDate", new Model(proj.getBeginDate()));
			final RequiredTextField budgetHour = new RequiredTextField("budgetHour", new Model((proj.getBudgetHour() == null)?"":proj.getBudgetHour()+""));
			budgetHour.add(new PatternValidator("^[-\\+]?\\d+(\\.\\d*)?|\\.\\d+$"));//是否为double
			final CheckBox isEnd = new CheckBox("isEnd", new Model(proj.getIsEnd() != null && proj.getIsEnd() != 0));
			final TextField endDate = new TextField("endDate", new Model(proj.getEndDate()));
			final TextArea memo = new TextArea("memo", new Model(proj.getMemo()));
			
			final Form projUpdateForm = new Form("projUpdateForm", new CompoundPropertyModel(bean)) {
				protected void onSubmit() {
					
					ProjectDTO formProj = new ProjectDTO();
					formProj.setProjectId((String)projectId.getModelObject());
					formProj.setProjName((String)projName.getModelObject());
					formProj.setTaskAddr((String)taskAddr.getModelObject());
					formProj.setBeginDate((String)beginDate.getModelObject());
					formProj.setBudgetHour(Double.parseDouble((String)budgetHour.getModelObject()));
					formProj.setIsEnd(((Boolean)isEnd.getModelObject()) ? 1 : 0);
					formProj.setEndDate((String)endDate.getModelObject());
					formProj.setMemo((String)memo.getModelObject());
					formProj.setDeptId((String)deptIdInput.getModelObject());
					formProj.setUpdateDate(UtilsMisc.getCurrTimeStr());
					
					if(StringUtils.isBlank(formProj.getProjectId())) {
						getMainService().getProjDao().create(formProj);
						info("新增项目成功");
					}
					else {
						getMainService().getProjDao().update(formProj);
						info("修改项目成功");
					}
				}
			};
			
			projUpdateForm.setOutputMarkupId(true);
			projUpdateForm.add(feedbackPanel);
			projUpdateForm.add(selectedDate);
			
			
			final DeptChoice depts = new DeptChoice("depts", getMainService().getJdbcTemplate(), getUser().getDeptId());
			depts.setOutputMarkupId(true);
			OnChangeAjaxBehavior changeDept = new OnChangeAjaxBehavior() {
				protected void onUpdate(AjaxRequestTarget target) {

					String deptId = depts.getModelValue();
					if(StringUtils.isNotBlank(deptId) && !"-1".equals(deptId)) {
						deptIdInput.setModel(new Model(deptId));
						deptNameInput.setModel(new Model(depts.getDeptName(deptId)));
					}
					else {
						deptIdInput.setModel(new Model(""));
						deptNameInput.setModel(new Model(""));
					}
					target.addComponent(deptIdInput);
					target.addComponent(deptNameInput);
				}
				
			};
			depts.add(changeDept);
			deptIdInput.setModel(new Model(proj.getDeptId()));
			deptNameInput.setModel(new Model(depts.getDeptName(proj.getDeptId())));
			
			projUpdateForm.add(depts);
			
			projUpdateForm.add(projName);
			projUpdateForm.add(projectId);
			projUpdateForm.add(taskAddr);
			projUpdateForm.add(deptIdInput);
			projUpdateForm.add(deptNameInput);
			projUpdateForm.add(beginDate);
			projUpdateForm.add(budgetHour);
			projUpdateForm.add(isEnd);
			projUpdateForm.add(endDate);
			projUpdateForm.add(memo);
			
			
			
			add(projUpdateForm);
		}
	}
	
	protected class ProjectListPanel extends Panel {
		
		final WebMarkupContainer projMngListPanel = new WebMarkupContainer("projMngListPanel");

		public ProjectListPanel(String id) {
			super(id);
			
			final FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
			feedbackPanel.setOutputMarkupId(true).setMarkupId("feedbackPanel");
			
			final Label selectedDate = new Label("selectedDate", "项目列表");
			selectedDate.setMarkupId("selectedDate");
			selectedDate.setOutputMarkupId(true);;
			
			final Form projMngForm = new Form("projMngForm", new CompoundPropertyModel(new ValueMap())) {
				protected void onSubmit() {
				}
			};
			
			projMngListPanel.setOutputMarkupId(true);
			projMngListPanel.add(new ListView("projList", projMngList) {
				protected void populateItem(final ListItem item) {
					
					
					item.add(new Label("projectIdx", (item.getIndex()+1)+""));
					
					item.add(new ExternalLink("projectName", new PropertyModel(item.getModel(), "TASK_ADDR"), new PropertyModel(item.getModel(), "PROJ_NAME")));
					item.add(new Label("beginDate", new PropertyModel(item.getModel(), "BEGIN_DATE")));
					item.add(new Label("milestone", new PropertyModel(item.getModel(), "CURR_MILESTONE")));
					item.add(new Label("allCost", new Model<String>(UtilsMisc.transHour(new PropertyModel(item.getModel(), "HOUR_COST").getObject()))));
					item.add(new Label("budgetHour", new PropertyModel(item.getModel(), "BUDGET_HOUR")));
					item.add(new Label("dept", new PropertyModel(item.getModel(), "DEPT_NAME")));
					item.add(new Label("pm", new PropertyModel(item.getModel(), "PM")));
					
					item.add(new AjaxButton("updateButt"){

						protected void onSubmit(AjaxRequestTarget target,Form form) {
							operPanel.replace(new ProjectUpdatePanel("operPanelContent", new PropertyModel(item.getModel(), "PROJECT_ID").getObject().toString()));
							target.addComponent(operPanel);
//							target.appendJavascript("$('#depts option')[0].selected = true;");
						}
					});
					item.add(new AjaxButton("subProjButt"){

						protected void onSubmit(AjaxRequestTarget target,Form form) {
							operPanel.replace(new SubProjPanel("operPanelContent", new PropertyModel(item.getModel(), "PROJECT_ID").getObject().toString()));
							target.addComponent(operPanel);
						}
					});
					item.add(new AjaxButton("milestoneButt"){

						protected void onSubmit(AjaxRequestTarget target,Form form) {
							operPanel.replace(new ProjMilestonePanel("operPanelContent", new PropertyModel(item.getModel(), "PROJECT_ID").getObject().toString()));
							target.addComponent(operPanel);
						}
					});
					AjaxButton pmButt = new AjaxButton("pmButt"){

						protected void onSubmit(AjaxRequestTarget target,Form form) {
							operPanel.replace(new ProjPMPanel("operPanelContent", new PropertyModel(item.getModel(), "PROJECT_ID").getObject().toString()));
							target.addComponent(operPanel);
						}
					};
					item.add(pmButt);
					AjaxSubmitLink delLink = new AjaxSubmitLink("delLink") {

						@Override
						protected void onSubmit(AjaxRequestTarget target,Form form) {
							
							String projId = new PropertyModel(item.getModel(), "PROJECT_ID").getObject().toString();
							
							for (Iterator iterator = projMngList.iterator(); iterator.hasNext();) {
								Map projMap = (Map) iterator.next();
								if(projId.equals(projMap.get("PROJECT_ID"))) {
									iterator.remove();
								}
							}
							
							getMainService().getProjDao().remove(projId);
							getMainService().getProjMilestoneDao().removeByCriteria("project_id = '"+projId+"'");
							getMainService().getUserProjDao().removeByCriteria("project_id = '"+projId+"'");
							getMainService().getUserHourDao().removeByCriteria("project_id = '"+projId+"'");
							target.addComponent(projMngListPanel);
						}

					};
					item.add(delLink);
					Button delButt = new Button("delButt"){};
					item.add(delButt);
					
					if(!SecurityFacade.isManager()) {
						pmButt.setVisible(false);
						delLink.setVisible(false);	
						delButt.setVisible(false);
					}
				}
				
			});
			projMngForm.add(projMngListPanel);
			
			projMngForm.add(feedbackPanel);
			projMngForm.add(selectedDate);
			add(projMngForm);
		}
		
	}

	protected class ProjMilestonePanel extends Panel {
		
		public ProjMilestonePanel(String id, String projId) {
			super(id);
			
			final ProjectDTO proj = (ProjectDTO)getMainService().getProjDao().findById(projId);
			proj.setMilestoneList(getMainService().getProjMilestoneDao(), projId);
			
			final FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
			feedbackPanel.setOutputMarkupId(true).setMarkupId("feedbackPanel");
			
			final Label selectedDate = new Label("selectedDate", "["+proj.getProjName()+"]里程碑/阶段列表");
			selectedDate.setMarkupId("selectedDate");
			selectedDate.setOutputMarkupId(true);;
			
			final HiddenField projectId = new HiddenField("projectId", new Model(proj.getProjectId()));
			final RequiredTextField mileNameInput = new RequiredTextField("mileNameInput", new Model<String>(""));
			mileNameInput.setOutputMarkupId(true);
			final RequiredTextField beginDateInput = new RequiredTextField("beginDateInput", new Model<String>(""));
			beginDateInput.setOutputMarkupId(true);
			final TextField memoInput = new TextField("memoInput", new Model<String>(""));
			memoInput.setOutputMarkupId(true);
			
			final Form projMilestoneForm = new Form("projMilestoneForm", new CompoundPropertyModel(new ValueMap())) {
				protected void onSubmit() {
					//do nothing
				}
			};
			
			final WebMarkupContainer milestoneListPanel = new WebMarkupContainer("milestoneListPanel");
			milestoneListPanel.setOutputMarkupId(true);
			milestoneListPanel.add(new ListView("milestoneList", proj.getMilestoneList()) {
				protected void populateItem(final ListItem item) {
					final ProjMilestoneDTO mile = (ProjMilestoneDTO)item.getModelObject();
					
					item.add(new Label("idx", (item.getIndex()+1)+""));
					
					item.add(new Label("mileName", new Model(mile.getMileName())));
					item.add(new Label("beginDate", new Model(mile.getBeginDate())));
					item.add(new Label("memo", new Model(mile.getMemo())));
					
					item.add(new AjaxLink("delLink") {

						@Override
						public void onClick(AjaxRequestTarget target) {
							getMainService().getProjMilestoneDao().remove(mile.getMilestoneId());
							proj.getMilestoneList().remove(mile);
							target.addComponent(milestoneListPanel);
						}

					});
				}
				
			});
			
			milestoneListPanel.add(mileNameInput);
			milestoneListPanel.add(beginDateInput);
			milestoneListPanel.add(memoInput);
			
			milestoneListPanel.add(new AjaxButton("addMileButt"){

				protected void onSubmit(AjaxRequestTarget target,Form form) {
					
					ProjMilestoneDTO newmile = new ProjMilestoneDTO();
					newmile.setBeginDate((String)beginDateInput.getModelObject());
					newmile.setMileName((String)mileNameInput.getModelObject());
					newmile.setMemo((String)memoInput.getModelObject());
					newmile.setProjectId((String)projectId.getModelObject());
					
					getMainService().getProjMilestoneDao().create(newmile);
					
					mileNameInput.setModel(new Model<String>(""));
					beginDateInput.setModel(new Model<String>(""));
					memoInput.setModel(new Model<String>(""));
					
					target.addComponent(mileNameInput);
					target.addComponent(beginDateInput);
					target.addComponent(memoInput);
					
					proj.getMilestoneList().add(newmile);
					
					//将项目的currMilestone的设为最新的阶段
					getMainService().getJdbcTemplate().update("update project set curr_milestone = (select mile_name from proj_milestone where begin_date = (select max(begin_date) from proj_milestone where project_id = '"+projectId.getModelObject()+"') limit 1) where project_id = '"+projectId.getModelObject()+"'");
					
					target.addComponent(milestoneListPanel);
				}
			});

			projMilestoneForm.add(projectId);
			projMilestoneForm.add(milestoneListPanel);
			
			projMilestoneForm.add(feedbackPanel);
			projMilestoneForm.add(selectedDate);
			
			add(projMilestoneForm);
		}
	}
	protected class ProjPMPanel extends Panel {
		
		public ProjPMPanel(String id, String projId) {
			super(id);
			
			final ProjectDTO proj = (ProjectDTO)getMainService().getProjDao().findById(projId);
			proj.setPmList(getMainService().getUserProjDao(), projId);
			
			final FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
			feedbackPanel.setOutputMarkupId(true).setMarkupId("feedbackPanel");
			
			final Label selectedDate = new Label("selectedDate", "["+proj.getProjName()+"]项目经理");
			selectedDate.setMarkupId("selectedDate");
			selectedDate.setOutputMarkupId(true);;
			
			final HiddenField projectId = new HiddenField("projectId", new Model(proj.getProjectId()));
			
			final StaffChoice staffs = new StaffChoice("staffs");
			staffs.setOutputMarkupId(true);
			staffs.getStaffDirectly(getMainService().getJdbcTemplate(), "1=1 order by name");
			
			final Form projPMForm = new Form("projPMForm", new CompoundPropertyModel(new ValueMap())) {
				protected void onSubmit() {
					//do nothting
				}
			};
			
			final WebMarkupContainer pmListPanel = new WebMarkupContainer("pmListPanel");
			pmListPanel.setOutputMarkupId(true);
			pmListPanel.add(new ListView("pmList", proj.getPmList()) {
				protected void populateItem(final ListItem item) {
					final UserProjDTO up = (UserProjDTO)item.getModelObject();
					
					item.add(new Label("idx", (item.getIndex()+1)+""));
					
					item.add(new Label("pmName", new Model(up.getName())));
					
					item.add(new AjaxLink("delLink") {

						@Override
						public void onClick(AjaxRequestTarget target) {
							getMainService().getUserProjDao().remove(up.getUserProjId());
							proj.getPmList().remove(up);

							//将项目的pm的设为冗余的项目经理
							String pms = "";
							for (Iterator iterator = proj.getPmList().iterator(); iterator
									.hasNext();) {
								UserProjDTO up = (UserProjDTO) iterator.next();
								pms += ","+up.getName();
							}
							if(pms.equals(""))
								pms = ",";
							getMainService().getJdbcTemplate().update("update project set pm = '"+pms.substring(1)+"' where project_id = '"+projectId.getModelObject()+"'");

							target.addComponent(pmListPanel);
						}

					});
				}
				
			});
			
			pmListPanel.add(staffs);
			
			pmListPanel.add(new AjaxButton("addPMButt"){

				protected void onSubmit(AjaxRequestTarget target,Form form) {

					String userId = staffs.getModelValue();
					if(StringUtils.isNotBlank(userId) && !"-1".equals(userId)) {

						List opmList = getMainService().getUserProjDao().queryByCriteria("user_id = '"+userId+"' and project_id = '"+projectId.getModelObject()+"'");
					
						if(CollectionUtils.isEmpty(opmList)) {
							UserProjDTO newpm = new UserProjDTO();
							newpm.setName(staffs.getStaffName(staffs.getModelValue()));
							newpm.setUserId(staffs.getModelValue());
							newpm.setProjectId((String)projectId.getModelObject());
							
							getMainService().getUserProjDao().create(newpm);
							
							proj.getPmList().add(newpm);
							
							//将项目的pm的设为冗余的项目经理
							String pms = "";
							for (Iterator iterator = proj.getPmList().iterator(); iterator
									.hasNext();) {
								UserProjDTO up = (UserProjDTO) iterator.next();
								pms += ","+up.getName();
							}
							getMainService().getJdbcTemplate().update("update project set pm = '"+pms.substring(1)+"' where project_id = '"+projectId.getModelObject()+"'");
							
							//如果不是项目经理的角色，则赋予上
							String ur = (String)getMainService().getJdbcTemplate().queryForObject("select user_role from user_info where user_id = ?", new Object[]{userId}, String.class);
							getMainService().getJdbcTemplate().update("update user_info set user_role = ? where user_id = ?", new Object[]{ur.substring(0,1)+"1"+ur.substring(2), userId});
						}
					}
					target.addComponent(pmListPanel);
				}
			});

			projPMForm.add(projectId);
			projPMForm.add(pmListPanel);
			
			projPMForm.add(feedbackPanel);
			projPMForm.add(selectedDate);
			
			add(projPMForm);
			
		}
	}
	
	protected class SubProjPanel extends Panel {
		
		public SubProjPanel(String id, String projId) {
			super(id);
			
			final ProjectDTO proj = (ProjectDTO)getMainService().getProjDao().findById(projId);
			proj.setSubProjList(getMainService().getProjDao(), projId);
			
			final FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
			feedbackPanel.setOutputMarkupId(true).setMarkupId("feedbackPanel");
			
			final Label selectedDate = new Label("selectedDate", "["+proj.getProjName()+"]子项目/任务列表");
			selectedDate.setMarkupId("selectedDate");
			selectedDate.setOutputMarkupId(true);;
			
			final HiddenField projectId = new HiddenField("projectId", new Model(proj.getProjectId()));
			final HiddenField subProjectIdInput = new HiddenField("subProjectIdInput");
			subProjectIdInput.setOutputMarkupId(true);
			final RequiredTextField projNameInput = new RequiredTextField("projNameInput", new Model<String>(""));
			projNameInput.setOutputMarkupId(true);
			final RequiredTextField beginDateInput = new RequiredTextField("beginDateInput", new Model<String>(""));
			beginDateInput.setOutputMarkupId(true);
			final RequiredTextField budgetHourInput = new RequiredTextField("budgetHourInput", new Model<String>(""));
			budgetHourInput.setOutputMarkupId(true);
			final TextField memoInput = new TextField("memoInput", new Model<String>(""));
			memoInput.setOutputMarkupId(true);
			
			final Form projSubProjForm = new Form("projSubProjForm", new CompoundPropertyModel(new ValueMap())) {
				protected void onSubmit() {
					//do nothing
				}
			};
			
			final WebMarkupContainer subProjListPanel = new WebMarkupContainer("subProjListPanel");
			subProjListPanel.setOutputMarkupId(true);
			subProjListPanel.add(new ListView("subProjList", proj.getSubProjList()) {
				protected void populateItem(final ListItem item) {
					final ProjectDTO subProj = (ProjectDTO)item.getModelObject();
					
					item.add(new Label("idx", (item.getIndex()+1)+""));
					item.add(new HiddenField("subProjectId", new Model(subProj.getProjectId())));
					item.add(new Label("projName", new Model(subProj.getProjName())));
					item.add(new Label("beginDate", new Model(subProj.getBeginDate())));
					item.add(new Label("budgetHour", new Model(subProj.getBudgetHour().toString())));
					item.add(new Label("memo", new Model(subProj.getMemo())));
					
					item.add(new AjaxLink("updateLink") {

						@Override
						public void onClick(AjaxRequestTarget target) {
							
							subProjectIdInput.setModel(new Model(subProj.getProjectId()));
							projNameInput.setModel(new Model(subProj.getProjName()));
							budgetHourInput.setModel(new Model(subProj.getBudgetHour().toString()));
							beginDateInput.setModel(new Model(subProj.getBeginDate()));
							memoInput.setModel(new Model(subProj.getMemo()));
							
							
							target.addComponent(subProjectIdInput);
							target.addComponent(projNameInput);
							target.addComponent(budgetHourInput);
							target.addComponent(beginDateInput);
							target.addComponent(memoInput);
						}

					});
					item.add(new AjaxLink("delLink") {

						@Override
						public void onClick(AjaxRequestTarget target) {
							getMainService().getProjDao().remove(subProj.getProjectId());
							proj.getMilestoneList().remove(subProj);
							target.addComponent(subProjListPanel);
						}

					});
				}
				
			});
			
			subProjListPanel.add(subProjectIdInput);
			subProjListPanel.add(projNameInput);
			subProjListPanel.add(beginDateInput);
			subProjListPanel.add(budgetHourInput);
			subProjListPanel.add(memoInput);
			
			subProjListPanel.add(new AjaxButton("addSubProjButt"){

				protected void onSubmit(AjaxRequestTarget target,Form form) {
					
					ProjectDTO newSubProj = new ProjectDTO();
					
					newSubProj.setBeginDate((String)beginDateInput.getModelObject());
					newSubProj.setProjName((String)projNameInput.getModelObject());
					newSubProj.setBudgetHour(Double.valueOf((String)budgetHourInput.getModelObject()));
					newSubProj.setMemo((String)memoInput.getModelObject());
					newSubProj.setFatherProid((String)projectId.getModelObject());
					newSubProj.setProjectId((String)subProjectIdInput.getModelObject());
					newSubProj.setDeptId(proj.getDeptId());
					newSubProj.setIsEnd(proj.getIsEnd());
					newSubProj.setUpdateDate(UtilsMisc.getCurrTimeStr());
					
					if(StringUtils.isBlank(newSubProj.getProjectId())) {
						getMainService().getProjDao().create(newSubProj);
						proj.getSubProjList().add(newSubProj);
					}
					else {
						getMainService().getProjDao().update(newSubProj);
						int nestedIdx = 0;
						for (Iterator iterator = proj.getSubProjList().iterator(); iterator
								.hasNext();) {
							ProjectDTO nestedProj = (ProjectDTO) iterator.next();
							if(nestedProj.getProjectId().equals(newSubProj.getProjectId())) {
								proj.getSubProjList().set(nestedIdx, newSubProj);
								break;
							}
							nestedIdx++;
						}
						
					}
					
					projNameInput.setModel(new Model<String>(""));
					beginDateInput.setModel(new Model<String>(""));
					budgetHourInput.setModel(new Model<String>(""));
					memoInput.setModel(new Model<String>(""));
					subProjectIdInput.setModel(new Model<String>(""));
					
					target.addComponent(subProjectIdInput);
					target.addComponent(projNameInput);
					target.addComponent(budgetHourInput);
					target.addComponent(beginDateInput);
					target.addComponent(memoInput);
					
					target.addComponent(subProjListPanel);
				}
			});

			projSubProjForm.add(projectId);
			projSubProjForm.add(subProjListPanel);
			
			projSubProjForm.add(feedbackPanel);
			projSubProjForm.add(selectedDate);
			
			add(projSubProjForm);
		}
	}
}
